/**
 * 66. Plus One
 * https://leetcode.com/problems/plus-one/
 */
package lc66;

public class Main {
  public static void main(String[] args) {
    int[] digits = {9,9};
    int[] arr = new Solution().plusOne(digits);
    for (int i : arr) {
      System.out.print(i);
    }
  }
}

class Solution {
  public int[] plusOne(int[] digits) {
    int n = digits.length;
    int carry = 1;
    while (--n >= 0) {
      digits[n] += carry;
      if (digits[n] > 9) {
        digits[n] -= 10;
      } else {
        return digits;
      }
    }

    int newLen = digits.length + 1;
    int[] newArr = new int[newLen];
    newArr[0] = 1;
    return newArr;
  }
}
